`
https://leetcode.cn/problems/linked-list-random-node/
`

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 */
var Solution = function (head) {
  this.head = head
};

/**
* @return {number}
*/
Solution.prototype.getRandom = function () {
  let i = 0;
  let res = 0;
  let p = this.head;

  // while 循环遍历链表
  while (p != null) {
    i++;

    // 生成一个 [0, i) 之间的整数
    // 这个整数等于 0 的概率就是 1/i
    if (0 === Math.floor(Math.random() * i)) {
      res = p.val;
    }
    p = p.next;
  }
  return res;
};

/** 
* Your Solution object will be instantiated and called as such:
* var obj = new Solution(head)
* var param_1 = obj.getRandom()
*/